
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
validate.js
Advanced tools
Validate.js is a lightweight JavaScript library for data validation. It provides a simple and flexible way to validate data structures, ensuring that they meet specified criteria. The library supports a wide range of validation rules and can be easily extended to include custom validations.
Basic Validation
This feature allows you to validate basic data structures against a set of predefined constraints. In this example, the 'name' field must be present and have a minimum length of 3 characters, and the 'email' field must be present and follow a valid email format.
const validate = require('validate.js');
const constraints = {
name: {
presence: true,
length: {
minimum: 3
}
},
email: {
presence: true,
email: true
}
};
const data = {
name: 'John Doe',
email: 'john.doe@example.com'
};
const validationResult = validate(data, constraints);
console.log(validationResult);
Custom Validation
This feature allows you to define custom validation rules. In this example, a custom validator is created to check if the 'customField' matches the expected value. If it doesn't, a custom error message is returned.
const validate = require('validate.js');
validate.validators.customValidator = function(value, options, key, attributes) {
if (value !== options.expectedValue) {
return options.message || `${key} is not valid`;
}
};
const constraints = {
customField: {
customValidator: {
expectedValue: 'expectedValue',
message: 'Custom validation failed'
}
}
};
const data = {
customField: 'wrongValue'
};
const validationResult = validate(data, constraints);
console.log(validationResult);
Asynchronous Validation
This feature supports asynchronous validation, which is useful for scenarios where validation depends on external data or processes. In this example, an asynchronous validator checks if the 'asyncField' matches the expected value after a delay.
const validate = require('validate.js');
validate.validators.asyncValidator = function(value, options, key, attributes) {
return new validate.Promise(function(resolve, reject) {
setTimeout(function() {
if (value !== options.expectedValue) {
resolve(options.message || `${key} is not valid`);
} else {
resolve();
}
}, 1000);
});
};
const constraints = {
asyncField: {
asyncValidator: {
expectedValue: 'expectedValue',
message: 'Asynchronous validation failed'
}
}
};
const data = {
asyncField: 'wrongValue'
};
validate.async(data, constraints).then(function(validationResult) {
console.log(validationResult);
});
Joi is a powerful schema description language and data validator for JavaScript. It allows you to create blueprints or schemas for JavaScript objects to ensure validation of key information. Compared to Validate.js, Joi offers a more extensive and expressive API for defining complex validation rules and is widely used in Node.js applications.
Yup is a JavaScript schema builder for value parsing and validation. It is similar to Joi but is often preferred for its simplicity and ease of use, especially in React applications. Yup provides a fluent API for object schema validation and integrates well with form libraries like Formik.
Validator is a library of string validators and sanitizers. It is less comprehensive than Validate.js, focusing primarily on string validation and sanitization. It is lightweight and easy to use for common validation tasks such as checking email formats, URLs, and other string-based validations.
Validate.js provides a declarative way of validating javascript objects.
For documentation please see validatejs.org/.
For issues and feature requests visit the issue tracker.
git clone git@github.com:ansman/validate.js.git
cd validate.js
npm install
grunt build
This will build validate.min.js, validate.min.map and the docs folder.
You can run continuous testing that runs the tests on file changes by running
grunt watch
or simply grunt
.
If you want to just run the tests once you can do that by running grunt test
.
Before opening a pull request please make sure your changes follow the contribution guidelines.
If your site, library or application uses validate.js and would like to be shown here please feel free to email info@validatejs.org with the name and optionally a URL to the project and it will be added here.
FAQs
Declarative validations for JavaScript
We found that validate.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.